package com.ita.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.dao.OrderDao;
import com.ita.dao.impl.OrderDaoImpl;
import com.ita.pojo.Cart;
import com.ita.pojo.Order;
import com.ita.pojo.User;

public class BuyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<Cart> carts = (List<Cart>) request.getSession().getAttribute("carts");
		User user = (User) request.getSession().getAttribute("user");
		String username = user.getUsername();
		if (carts == null) {

			response.sendRedirect("shopcart.jsp");
		} else {

			OrderDao orderDao = new OrderDaoImpl();
			for (Cart cart : carts) {
				Order order = new Order(UUID.randomUUID() + "", username,
						cart.getId(), cart.getBookname(), Integer.parseInt(cart
								.getBooknum()), new Date());
				orderDao.addOrder(order);
			}
			request.setAttribute("msg", "buy success");
			request.getRequestDispatcher("clear").forward(request, response);

		}

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
